﻿Imports Training.Common

Partial Public Class AddModule
    Inherits System.Web.UI.Page
    Dim mdid As Integer ' ModuleID

    ''' <summary>
    ''' Page load event, bind datasources for listboxes
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            LoadQuizzes()
            LoadVideos()
        End If
    End Sub

    ''' <summary>
    ''' Bind datasource for lbQuiz ListBox
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadQuizzes()
        lbQuiz.DataSource = GetAllQuizzes()
        lbQuiz.DataTextField = "QuizName"
        lbQuiz.DataValueField = "QuizID"
        lbQuiz.DataBind()
    End Sub

    ''' <summary>
    ''' Bind datasource for lbVideo ListBox
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadVideos()
        lbVideo.DataSource = GetAllVideos()
        lbVideo.DataTextField = "VideoName"
        lbVideo.DataValueField = "VideoID"
        lbVideo.DataBind()
    End Sub

    ''' <summary>
    ''' Add Button click event, check for data and then add module
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If Page.IsValid Then
            ' check for selections in listboxes
            If CheckListBoxes() Then
                ' add module and get ID back
                mdid = CreateModule(tbMName.Text)
                ' add quizzes for module
                AddTheQuizzes(mdid)
                ' add videos for module
                AddTheVideos(mdid)
            Else
                ' message to user
                lblMsg.Text = "You must select at least one Quiz and one Video"
            End If
        Else
            lblMsg.Text = "Validation of the form failed. Please try again."
        End If
    End Sub

    ''' <summary>
    ''' Check listboxes and make sure selections were made
    ''' </summary>
    ''' <returns>Boolean</returns>
    ''' <remarks></remarks>
    Private Function CheckListBoxes() As Boolean
        Dim retVal As Boolean

        If lbQuiz.SelectedValue = "" Or lbVideo.SelectedValue = "" Then
            retVal = False
        Else
            retVal = True
        End If

        Return retVal
    End Function

    ''' <summary>
    ''' Create Module and return ModuleID
    ''' </summary>
    ''' <param name="mnm"></param>
    ''' <returns>Integer of ModuleID</returns>
    ''' <remarks></remarks>
    Private Function CreateModule(ByVal mnm As String) As Integer
        main.Visible = False
        btnAdd.Visible = False
        resp.Visible = True
        lblMsg.Visible = False
        lblResponse.Text = "Module -" & tbMName.Text & "- has been added."
        Return AddModuleGetID(tbMName.Text)
    End Function

    ''' <summary>
    ''' Add Quizzes for Module
    ''' </summary>
    ''' <param name="mdid"></param>
    ''' <remarks></remarks>
    Private Sub AddTheQuizzes(ByVal mdid As Integer)
        Dim qItem As ListItem
        For Each qItem In lbQuiz.Items
            If qItem.Selected = True Then
                AddQuizToModule(mdid, qItem.Value)
            End If
        Next
    End Sub

    ''' <summary>
    ''' Add Videos for Module
    ''' </summary>
    ''' <param name="mdid"></param>
    ''' <remarks></remarks>
    Private Sub AddTheVideos(ByVal mdid As Integer)
        Dim vItem As ListItem
        For Each vItem In lbVideo.Items
            If vItem.Selected = True Then
                AddVideoToModule(mdid, vItem.Value)
            End If
        Next
    End Sub

    ''' <summary>
    ''' Redirect to add a collateral file
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFile.Click
        Server.Transfer("AddFileForm.aspx")
    End Sub
End Class